package com.knoya.chejian.pda.module.chache

import android.app.ActivityManager
import android.app.AlertDialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.view.View
import com.knoya.chejian.pda.R
import com.knoya.chejian.pda.activity.BaseActivity
import com.knoya.chejian.pda.minterface.OnUserDataInterface
import com.knoya.chejian.pda.util.Myutil
import kotlinx.android.synthetic.main.activity_chache.*
import kotlinx.android.synthetic.main.header_knoya.*

class ChaCheActivity : BaseActivity(), View.OnClickListener {

    lateinit var M : ChacheM
    lateinit var dialog: AlertDialog
    override fun getLayoutId(): Int {
        return R.layout.activity_chache
    }

    override fun initCreate() {
        M = ChacheM()
        iv_header_left.visibility = View.VISIBLE
        iv_header_left.setOnClickListener(this)
        tv_header_title.text = "叉车上传"
        Bt_RuLiku.setOnClickListener(this)
        Bt_RuHuanchong.setOnClickListener(this)

        mBoradcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                if(!isAPPBroughtToBackground()){
                    return
                }
                if (intent.action == "nlscan.action.SCANNER_RESULT" && intent.hasExtra("SCAN_BARCODE1")) {
                    val scanResult_1 = intent.getStringExtra("SCAN_BARCODE1").trim()
                    samiaoOk(scanResult_1)
                }
                if(intent.action == "android.intent.ACTION_DECODE_DATA" && intent.hasExtra("barcode_string")){
                    val scanResult = intent.getStringExtra("barcode_string").trim()
                    samiaoOk(scanResult)
                }
                if(intent.action == "com.qs.scancode"){
                    val scanResult = intent.extras.getString("data").trim()
                    samiaoOk(scanResult)
                }
            }
        }
    }

    //扫描获取到数据后处理
    fun samiaoOk(d : String){
        var data = Myutil.fengehtttppcode(d)
        Tv_PalletNum.text = data;
    }

    override fun onClick(view: View?) {
        when(view){
            iv_header_left -> {
                finish()
            }
            Bt_RuLiku -> {
                if(Tv_PalletNum.text.toString().trim() == ""){
                    return
                }
                dialog = Myutil.showupdatedialog(this@ChaCheActivity, "正在上传叉车数据", false)
                var uid = sp.getString("uid","")
                M.chuku(this,Tv_PalletNum.text.toString(),1,uid,object :OnUserDataInterface{
                    override fun onUserData(data: String?) {
                        Tv_PalletNum.text = ""
                        Myutil.MyTost("数据上传成功")
                        dialog.dismiss()
                    }

                    override fun onNot(throwable: String?) {
                        dialog.dismiss()
                    }

                })
            }
            Bt_RuHuanchong -> {
                if(Tv_PalletNum.text.toString().trim() == ""){
                    return
                }
                dialog = Myutil.showupdatedialog(this@ChaCheActivity, "正在上传叉车数据", false)
                var uid = sp.getString("uid","")
                M.chuku(this,Tv_PalletNum.text.toString(),2,uid,object :OnUserDataInterface{
                    override fun onUserData(data: String?) {
                        Tv_PalletNum.text = ""
                        Myutil.MyTost("数据上传成功")
                        dialog.dismiss()
                    }

                    override fun onNot(throwable: String?) {
                        dialog.dismiss()
                    }

                })
            }
        }
    }
    fun isAPPBroughtToBackground(): Boolean {
        val am = this.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val tasks = am.getRunningTasks(1)
        if (!tasks.isEmpty()) {
            val topActivity = tasks[0].topActivity
            if (topActivity.className == this.javaClass.name) {

                return true
            }
        }
        return false
    }
}